package com.zqq.goods.presenter

import com.zqq.baselibrary.ext.excute
import com.zqq.baselibrary.http.rx.BaseSubscriber
import com.zqq.baselibrary.presenter.BasePresenter
import com.zqq.goods.presenter.view.CategoryView
import com.zqq.goods.service.CategoryService
import com.zqq.goods.service.bean.Category
import javax.inject.Inject

/**
 * 商品分类 Presenter
 * wc
 * @author zqq on 2018/8/1
 */
class CategoryPresenter @Inject constructor() : BasePresenter<CategoryView>() {

    @Inject
    lateinit var categoryService: CategoryService

    /**
     * 1.获取商品分类列表
     */
    fun getCategory(parentId: Int) {
        if (!checkNetWork()) {
            return
        }
        mView.showLoading()
        categoryService.getCategory(parentId)
                .excute(object : BaseSubscriber<MutableList<Category>?>(mView) {
                    override fun onNext(t: MutableList<Category>?) {
                        mView.onGetCategoryResult(t)
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        //测试
                        var lists = mutableListOf<Category>()
                        var img1 = "https://graph.baidu.com/resource/1014e21817bd3240b7d4201559113677.jpg"
                        var img2 = "https://graph.baidu.com/resource/101f10c9ec8512b9f724901559113840.jpg"
                        var img3 = "https://graph.baidu.com/resource/101cab8039a241b38173c01559113919.jpg"
                        if (parentId == 0) {
                            lists.clear()
                            var cate1 = Category(1, "第一个", img1, 0, true)
                            var cate3 = Category(3, "第三个", img3, 0, false)
                            lists.add(cate1)
                            lists.add(cate3)
                        } else {
                            lists.clear()
                            var cate2 = Category(2, "第二个", img2, 1, false)
                            var cate4 = Category(4, "第四个", img1, 3, false)
                            lists.add(cate2)
                            lists.add(cate4)
                        }

                        mView.onGetCategoryResult(lists)
                    }
                }, lifecycleProvider)
    }


}